<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *INVISI
*/

class Serahterima_ppk_model extends CI_Model
{	
	/**
	* constructor
	*/
    public function __construct()    {
        parent::__construct();
		//$this->CI =& get_instance();
    }
	
    public function easyGrid($purpose=1,$filtipe,$filawal,$filakhir){
         $lastNo = isset($_POST['lastNo']) ? intval($_POST['lastNo']) : 0;
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
        $limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;  

        $count = $this->GetRecordCount($filtipe,$filawal,$filakhir);
        $response = new stdClass();
        $response->total = $count;
        $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nomor';  
        $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
        $offset = ($page-1)*$limit;  
        $pdfdata = array();
          $jumlah =0;
        if ($count>0){
            
            if($filawal != '' && $filawal != '-1' && $filawal != null) {
                $this->db->where("s.tanggal between '$filawal' and '$filakhir'");
            }
            if($filtipe != '' && $filtipe != '-1' && $filtipe != null) {
                $this->db->where("s.tipe",$filtipe);
            } 
		 
		 
            $this->db->order_by($sort." ".$order );
            if($purpose==1){$this->db->limit($limit,$offset);}
            $this->db->select("s.*  ",false);
            $this->db->from('tbl_serahterima_ppk s'
                    ,false);
            $query = $this->db->get();
          
            $i=0;
            $no =$lastNo;
            foreach ($query->result() as $row)            {
                $no++;
                $response->rows[$i]['no']= $no;
                $response->rows[$i]['serahterima_id']=$row->serahterima_id;
                $response->rows[$i]['nomor']=$row->nomor;
                $response->rows[$i]['tanggal']=$this->utility->ourFormatDate2($row->tanggal);
                $response->rows[$i]['tipe']=$row->tipe;                                
                $response->rows[$i]['tipe_text']=($row->tipe=='serah'?'Serah ke PPK':'Terima dari PPK');                                
                $response->rows[$i]['keterangan']=$row->keterangan;
                $response->rows[$i]['penyerah']=$row->penyerah;
                $response->rows[$i]['penerima']=$row->penerima;
                
               
             //   $response->rows[$i]['jumlah']=$row->jumlah; //$this->utility->ourFormatNumber($row->jumlah);
             //   $jumlah += $row->jumlah;
                //utk kepentingan export pdf===================
				if ($purpose==2){
					$pdfdata[] = array($no,$response->rows[$i]['tanggal'],$response->rows[$i]['nomor'],$response->rows[$i]['tipe'],$response->rows[$i]['keterangan'],$response->rows[$i]['serahterima_id'],$response->rows[$i]['penyerah'],$response->rows[$i]['penerima']);
						
				}
        //============================================================
				//utk kepentingan export excel ==========================
				$row->serahterima_id = $no;
				//$row->bidang = $response->rows[$i]['bidang'];
				
				unset($row->tipe);
				unset($row->log_insert);
				unset($row->log_update);
			//	unset($row->bidang_id);
				
				
		
                $i++;
            } 

            $response->lastNo = $no;
                //$query->free_result();
        }else {
            $response->rows[$count]['no']= '';
            $response->rows[$count]['serahterima_id']= '';
            $response->rows[$count]['nomor']='';
            $response->rows[$count]['tanggal']='';
            $response->rows[$count]['tipe']='';
            $response->rows[$count]['tipe_text']='';
            $response->rows[$count]['keterangan']='';
            $response->rows[$count]['penyerah']='';
            $response->rows[$count]['penerima']='';
            
            $response->lastNo = 0;	
        }
        
        
        if ($purpose==1) //grid normal
            return json_encode($response);
        else if($purpose==2){//pdf
            return $pdfdata;
        }
        else if($purpose==3){//to excel
                //tambahkan header kolom
            $colHeaders = array("No","Tanggal","Nomor Tanda Terima","Bidang","Keterangan");		
        //    var_dump($query);die;
            to_excel($query,"TandaTerima",$colHeaders);
        }
        else if ($purpose==4) { //WEB SERVICE
            return $response;
        }

    }
	
	public function easyGridDetail($purpose=1,$tipe,$serahterima_id,$forInput=false){
        
        $response = new stdClass();
        
        
        $pdfdata = array();
        
            
        
	 
			if ($tipe=="serah")	$this->db->where("p.ppk_serah_id",$serahterima_id);
			if ($tipe=="terima") $this->db->where("p.ppk_terima_id",$serahterima_id);
	     
           
            $this->db->order_by("spb_id");
            
            $this->db->select("p.spb_id,p.tanggal,p.nomor,p.jumlah,p.kategori_id,k.kategori,p.untuk,p.bidang_id,b.bidang,p.tujuan,p.beban_kode, p.beban_kegiatan",false);
            $this->db->from('tbl_spb p  left join tbl_spb_kategori k on p.kategori_id = k.kategori_id '
                    . 'left join tbl_bidang b on p.bidang_id = b.bidang_id ',false);
            $query = $this->db->get();
          
            $i=0;
            $no=0;
            foreach ($query->result() as $row)            {
                $no++;
                $response->rows[$i]['no']= $no;
                //$response->rows[$i]['detail_id']=$row->detail_id;
                //$response->rows[$i]['serahter_id']=$row->tanda_id;
                $response->rows[$i]['spb_id']=$row->spb_id;
                $response->rows[$i]['nomor']=$row->nomor;
                if ($forInput){
                    $response->rows[$i]['jumlah']=$row->jumlah;
                }
                else {
                    $response->rows[$i]['jumlah']=$this->utility->ourFormatNumber($row->jumlah);    
                }
                
                
                $response->rows[$i]['kategori']=$row->kategori;
                $response->rows[$i]['untuk']=$row->untuk;
                $response->rows[$i]['tanggal']=$this->utility->ourFormatDate2($row->tanggal);                
                $response->rows[$i]['bidang_id']=$row->bidang_id;
                $response->rows[$i]['bidang']=$row->bidang;                
                $response->rows[$i]['kategori_id']=$row->kategori_id;
                $response->rows[$i]['tujuan']=$row->tujuan;
                $response->rows[$i]['beban_kegiatan']=$row->beban_kegiatan;
                $response->rows[$i]['beban_kode']=$row->beban_kode;
                $response->rows[$i]['kegiatan']='['.$row->beban_kode.']'.$row->beban_kegiatan;
                
                //utk kepentingan export pdf===================
                 $pdfdata[] = array($no,$response->rows[$i]['nomor'],$response->rows[$i]['tanggal'],$response->rows[$i]['bidang'],$response->rows[$i]['kategori'],$response->rows[$i]['jumlah']);
        //============================================================
                $i++;
            } 

            $response->lastNo = $no;
                //$query->free_result();
       if ($no==0) {
            $count =0;
            $response->rows[$count]['no']= '';
            $response->rows[$count]['detail_id']= '';
            $response->rows[$count]['tanda_id']= '';
            $response->rows[$count]['spb_id']= '';
            $response->rows[$count]['nomor']='';
            $response->rows[$count]['tanggal']='';
            $response->rows[$count]['jumlah']='';
            $response->rows[$count]['kategori']='';
            $response->rows[$count]['untuk']='';
            $response->rows[$count]['tanggal']='';                
            $response->rows[$count]['bidang_id']='';
            $response->rows[$count]['bidang']='';                
            $response->rows[$count]['kategori_id']='';
            $response->rows[$count]['tujuan']='';
            $response->rows[$count]['beban_kegiatan']='';
            $response->rows[$count]['beban_kode']='';
            $response->rows[$count]['kegiatan']='';
        }
        
        
        if ($purpose==1) //grid normal
            return json_encode($response);
        else if($purpose==2){//pdf
            return $pdfdata;
        }
        else if($purpose==3){//to excel
                //tambahkan header kolom
            $colHeaders = array("Kode","Nama Kementerian","Singkatan","Nama Menteri");		
            //var_dump($query->result());die;
            to_excel($query,"Kementerian",$colHeaders);
        }
        else if ($purpose==4) { //WEB SERVICE
            return $response;
        }

    }
	
	
	 public function easyGridInput($purpose=1,$tipe,$filserahterima_id=null){
        
        $response = new stdClass();
        
        $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nomor';  
        $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
       
          $jumlah =0;
       
            
        $view = 'v_spb_ppk_serah';
            //if($tipe != '' && $tipe != '-1' && $tipe != null) {
                // if ($tipe=="serah")	$this->db->where("s.ppk_serah_id is null");
                // else 
				if ($tipe=="terima")	{
				//	$this->db->where("s.ppk_serah_id is not null");
					$view = 'v_spb_ppk_terima';
				}
				//else $this->db->where("s.ppk_serah_id",$tipe);
					
            //}
           //$this->db->where("s.spb_id not in (select spb_id from tbl_tanda_terima_detail)");
		   if ($tipe=="-1"){ //default grid input kosongkan
			$this->db->where("s.ppk_serah_id","-1");
		   }
            $this->db->order_by($sort." ".$order );
            //if($purpose==1){$this->db->limit($limit,$offset);}
             $this->db->select("s.* ",false);
            $this->db->from($view.' s  ',false);
            $query = $this->db->get();
          
            $i=0;
            $no =0;//$lastNo;
            foreach ($query->result() as $row)            {
                $no++;
                $response->rows[$i]['no']= $no;
                $response->rows[$i]['spb_id']=$row->spb_id;
                $response->rows[$i]['nomor']=$row->nomor;
                $response->rows[$i]['tanggal']=$this->utility->ourFormatDate2($row->tanggal);
                $response->rows[$i]['bidang_id']=$row->bidang_id;
                $response->rows[$i]['bidang']=$row->bidang;
                $response->rows[$i]['kategori']=$row->kategori;
                $response->rows[$i]['kategori_id']=$row->kategori_id;
                $response->rows[$i]['tujuan']=$row->tujuan;
                $response->rows[$i]['untuk']=$row->untuk;
                $response->rows[$i]['beban_kegiatan']=$row->beban_kegiatan;
                $response->rows[$i]['beban_kode']=$row->beban_kode;
                $response->rows[$i]['kegiatan']='['.$row->beban_kode.']'.$row->beban_kegiatan;
                
               
                $response->rows[$i]['jumlah']=$row->jumlah; //$this->utility->ourFormatNumber($row->jumlah);
                $jumlah += $row->jumlah;
                //utk kepentingan export pdf===================
              // $pdfdata[] = array($no,$response->rows[$i]['nomor'],$response->rows[$i]['tujuan'],$response->rows[$i]['untuk'],$response->rows[$i]['beban_kegiatan']);
        //============================================================
                $i++;
            } 
            if ($no==0){
                $count=0;
                $response->rows[$count]['no']= '';
                $response->rows[$count]['sb_id']= '';
                $response->rows[$count]['nomor']='';
                $response->rows[$count]['tanggal']='';
                $response->rows[$count]['bidang_id']='';
                $response->rows[$count]['bidang']='';
                $response->rows[$count]['kategori']='';
                $response->rows[$count]['kategori_id']='';
                $response->rows[$count]['tujuan']='';
                $response->rows[$count]['untuk']='';
                $response->rows[$count]['beban_kegiatan']='';
                $response->rows[$count]['beban_kode']='';
                $response->rows[$count]['kegiatan']='';
                $response->rows[$count]['status_verifikasi']='';
                $response->rows[$count]['status_verifikasi_tanggal']='';
                $response->rows[$count]['status_verifikasi_oleh']='';
                $response->rows[$count]['status_penguji']='';
                $response->rows[$count]['status_penguji_tanggal']='';
                $response->rows[$count]['status_penguji_oleh']='';
                $response->rows[$count]['status_spm']='';
                $response->rows[$count]['status_spm_tanggal']='';
                $response->rows[$count]['status_spm_oleh']='';
                $response->rows[$count]['status_bendahara']='';
                $response->rows[$count]['status_bendahara_tanggal']='';
                $response->rows[$count]['status_bendahara_oleh']='';
                $response->rows[$count]['jumlah']='';
                $response->lastNo = 0;	
            }
            $response->lastNo = $no;
                //$query->free_result();
       
        
        
       // if ($purpose==1) //grid normal
            return json_encode($response);
       

    }
	
	//jumlah data record buat paging
    public function GetRecordCount($filtipe,$filawal,$filakhir){
		 
			
      if($filawal != '' && $filawal != '-1' && $filawal != null) {
                $this->db->where("s.tanggal between '$filawal' and '$filakhir'");
            }
            if($filtipe != '' && $filtipe != '-1' && $filtipe != null) {
                $this->db->where("s.tipe",$filtipe);
            } 
			  $this->db->from('tbl_serahterima_ppk s'
                    ,false);
        //$query=$this->db->from($viewname);
        return $this->db->count_all_results();
        $this->db->free_result();
    }
	
	
	public function selectById($id){
        $this->db->select("*", false);
        $this->db->from('tbl_serahterima_ppk');
        $this->db->where('serahterima_id',$id);

        $query = $this->db->get();
        return $query->row();
    }

    public function isExistKode($kode=null){	
        if ($kode!=null)//utk update
            $this->db->where('nomor',$kode); //buat validasi

        $this->db->select('*');
        $this->db->from('tbl_spb');

        $query = $this->db->get();
        $rs = $query->num_rows() ;		
        $query->free_result();
        return ($rs>0);
    }
	
	 
	
    public function isSaveDelete($kode){			
        /*$this->db->where('nomor',$kode); //buat validasi		
        $this->db->select('*');
        $this->db->from('tbl_sasaran_kl');

        $query = $this->db->get();
        $rs = $query->num_rows() ;		
        $query->free_result();
        $isSave = ($rs==0);
        if ($isSave){
            $this->db->flush_cache();
            $this->db->where('nomor',$kode); //buat validasi		
            $this->db->select('*');
            $this->db->from('tbl_iku_kl');

            $query = $this->db->get();
            $rs = $query->num_rows() ;		
            $query->free_result();
            $isSave = ($rs==0);
        }*/
        $isSave = true;
        return $isSave;
    }
	
	 public function InsertOnDb($data,& $error, & $serahterima_id) {
            //query insert data		
        $this->db->trans_start();
		//02-02-2015
		$bulan = substr($data['tanggal'],-7,-5);
		$tahun = substr($data['tanggal'],-4);
		$data['nomor'] = $this->getNewNumber($data['tipe'],$bulan,$tahun,false);
        $this->db->set('tanggal',$this->utility->ourDeFormatSQLDate($data['tanggal']));
        $this->db->set('tipe',$data['tipe']);
        $this->db->set('nomor',$data['nomor']);
        
        $this->db->set('keterangan',$data['keterangan']);
        $this->db->set('penerima',$data['penerima']);
        $this->db->set('penyerah',$data['penyerah']);
      
        $this->db->set('log_insert', 		$this->session->userdata('user_id').';'.date('Y-m-d H:i:s'));

        $result = $this->db->insert('tbl_serahterima_ppk');
        $serahterima_id = $this->db->insert_id();
        $errNo   = $this->db->_error_number();
        $errMess = $this->db->_error_message();
        $dataspb = explode(',', $data['spb_ids']);
        for ($i=0;$i<count($dataspb);$i++){
            $this->db->flush_cache();
			if ($data['tipe']=="serah") $this->db->set('ppk_serah_id',$serahterima_id);
			if ($data['tipe']=="terima") $this->db->set('ppk_terima_id',$serahterima_id);
			
            $this->db->where('spb_id',$dataspb[$i]);
            $this->db->update('tbl_spb');
        }
            $error = $errMess;
            //var_dump($errMess);die;
      //  log_message("error", "Problem Inserting to : ".$errMess." (".$errNo.")"); 
            //return
        $this->db->trans_complete();
	return $this->db->trans_status();
    }

	//update data
    public function UpdateOnDb($data, $serahterima_id) {
        $this->db->where('serahterima_id',$serahterima_id);
        //query insert data		
        $this->db->set('nomor',$data['nomor']);
        $this->db->set('tanggal',$this->utility->ourDeFormatSQLDate($data['tanggal']));
        $this->db->set('tipe',$data['tipe']);
        $this->db->set('keterangan',$data['keterangan']);
		$this->db->set('penerima',$data['penerima']);
        $this->db->set('penyerah',$data['penyerah']);
        $this->db->set('log_update', 		$this->session->userdata('user_id').';'.date('Y-m-d H:i:s'));

        $result=$this->db->update('tbl_serahterima_ppk');

        $errNo   = $this->db->_error_number();
        $errMess = $this->db->_error_message();
            //var_dump($errMess);die;
            //return
        if($result) {
            return TRUE;
        }else {
            return FALSE;
        }
    }
    
    
	
	//hapus data
    public function DeleteOnDb($id){
        $this->db->flush_cache();
        $this->db->where('spb_id', $id);
        $result = $this->db->delete('tbl_spb'); 

        $errNo   = $this->db->_error_number();
        $errMess = $this->db->_error_message();
        $error = $errMess;
        //var_dump($errMess);die;
       
		//return
		
        if($result) {
            return TRUE;
        }else {
             log_message("error", "Problem Update to : ".$errMess." (".$errNo.")"); 
            return FALSE;
        }
    }
	
   
    
	 
	function getNewNumber($tipe,$bln,$tahun,$isAjax=true){
       if ($tipe=="serah")
			$prefix = 'S-PPK'."/".$bln."/".$tahun.'/';
		else if ($tipe=="terima")
			$prefix = 'T-PPK'."/".$bln."/".$tahun.'/';
		else	
			$prefix = 'ERRRROR';
       $no= $this->utility->ourGetNextIDNum("nomor","tbl_serahterima_ppk"," and date_format(tanggal,'%m')= '$bln' and  date_format(tanggal,'%Y')='$tahun'",
               $prefix,"",4);
		if ($isAjax)	   
			echo $no;
		else
			return $no;
   }
	
    
}
?>
